package com.matriq.cloudmanager.fragment;

import java.util.Arrays;
import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.ConsoleDetailActivity;
import com.matriq.cloudmanager.entity.Console;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import net.lgpage.fragment.BaseFragment;
import net.lgpage.http.ExceptionAsyncTask;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Cleaner;
import net.lgpage.util.ObjectRandom;

public class ConsoleFragment extends BaseFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fm_console, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setViews();
		setEvents();
		loadData();
	}

	android.support.v4.widget.SwipeRefreshLayout srl;
	android.support.v7.widget.RecyclerView rcy_console;

	private void setViews() {
		srl = (android.support.v4.widget.SwipeRefreshLayout) getView().findViewById(R.id.srl);
		rcy_console = (android.support.v7.widget.RecyclerView) getView().findViewById(R.id.rcy_console);
		rcy_adp = new RcyAdp();
		rcy_console.setLayoutManager(new LinearLayoutManager(getActivity()));
		rcy_console.setAdapter(rcy_adp);
	}

	private void setEvents() {
		srl.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				refeshRcyList();
			}
		});
	}

	private class RcyViewHolder extends RecyclerView.ViewHolder {
		ImageView iv;
		TextView tv[] = new TextView[3];

		public RcyViewHolder(View view) {
			super(view);
		}
	}

	RcyAdp rcy_adp;

	class RcyAdp extends RecyclerView.Adapter<RcyViewHolder> {

		LayoutParams lp;

		@Override
		public int getItemCount() {
			if (rcylist == null)
				return 0;
			return rcylist.size();
		}

		public RcyAdp() {
			super();
			lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}

		@Override
		public void onBindViewHolder(RcyViewHolder h, final int position) {
			final Console obj = rcylist.get(position);
			if(TextUtils.isEmpty(obj.getHeadpic())){
				h.iv.setVisibility(View.GONE);
			}else{
				h.iv.setVisibility(View.INVISIBLE);
				loadImage(obj.getHeadpic(), h.iv);
			}
			h.tv[0].setText(obj.getTitle());
			h.tv[1].setText(obj.getContent());
			h.tv[2].setText(obj.getPutTime());
			h.itemView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					toActivity(ConsoleDetailActivity.class, obj);
				}
			});
		}

		@SuppressLint("InflateParams")
		@Override
		public RcyViewHolder onCreateViewHolder(ViewGroup vgp, final int position) {
			View cv = getActivity().getLayoutInflater().inflate(R.layout.rcy_console, null);
			cv.setLayoutParams(lp);
			RcyViewHolder h = new RcyViewHolder(cv);
			h.iv = (ImageView) cv.findViewById(R.id.iv);
			h.tv[0] = (TextView) cv.findViewById(R.id.tv0);
			h.tv[1] = (TextView) cv.findViewById(R.id.tv1);
			h.tv[2] = (TextView) cv.findViewById(R.id.tv2);
			return h;
		}
	}

	List<Console> rcylist;
	RcyListTask rcylistTask;

	public void refeshRcyList() {
		if (rcylistTask != null) {
			rcylistTask.cancel(true);
		}
		rcylistTask = new RcyListTask(getActivity());
		rcylistTask.setSwipeLayout(srl);
		rcylistTask.execute();
	}

	private class RcyListTask extends SwipeRefeshAsyncTask<List<Console>> {

		public RcyListTask(Context context) {
			super(context);
		}

		@Override
		public List<Console> doInBackgroundX(String... params) throws Exception {
			return ObjectRandom.randomList(Console.class);
		}

		@Override
		public void onPostExecuteX(List<Console> result) {
			if (result == null) {
				return;
			} else {
				rcylist = result;
				rcy_adp.notifyDataSetChanged();
			}
		}
	}

	private void loadData() {
		refeshRcyList();
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(rcylistTask);
		super.onDestroy();
	}

}